package com.mall.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.user.AddressService;
import com.mall.custom.dao.read.AddressReadMapper;
import com.mall.custom.dao.write.AddressWriteMapper;
import com.mall.enums.AddressEnum;
import com.mall.pojo.Address;
import com.mall.pojo.AddressExample;
import com.mall.pojo.AddressExample.Criteria;

@Service
public class AddressServiceImpl implements AddressService {
	@Autowired
	private AddressWriteMapper  addressWriteMapper;
	@Autowired
	private AddressReadMapper addressReadMapper;
	@Override
	public List<Address> getAddressLsitByExample(AddressExample example) {
		
		return this.addressReadMapper.selectByExample(example);
	}
	@Override
	public Address getAddressById(Long addressId) {
		// TODO Auto-generated method stub
		return this.addressReadMapper.selectByPrimaryKey(addressId);
	}
	@Override
	public int insertAddress(Address address) {
		return this.addressWriteMapper.insert(address);
		
	}
	@Override
	public int modifyAddressByPrimaryKey(Address addressMo) {
		return	this.addressWriteMapper.updateByPrimaryKey(addressMo);
		
	}
	@Override
	public Address getDefaultAddressByUserId(Long id) {
		if(id == null || id <= 0){
			return null;
		}
		AddressExample example = new AddressExample();
		 Criteria criteria = example.createCriteria();
		 criteria.andStatusEqualTo(AddressEnum.DEFAULTADDRESS.getCode());
		 criteria.andUserIdEqualTo(id);
		 List<Address> addressList = this.addressReadMapper.selectByExample(example);
		 Address address = null;
		 if(addressList !=null && addressList.size()>0){
			 address = addressList.get(0);
		 }
		return address;
	}

}
